package org.example.memora.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.utils.TagExtractor;
import org.example.memora.entity.HashTag;
import org.example.memora.mapper.HashtagMapper;
import org.example.memora.service.HashTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class HashTagServiceImpl implements HashTagService {
    private final HashtagMapper hashtagMapper;
    @Autowired
    private TagExtractor tagExtractor;

    @Override
    public boolean existsAllByIds(Iterable<Integer> tagIds) {
        List<Integer> idList = new ArrayList<>();
        tagIds.forEach(idList::add);

        if (idList.isEmpty()) {
            return true;
        }

        // 构造查询
        QueryWrapper<HashTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("hashtag_id", idList);

        // 查询匹配数量
        int count = Math.toIntExact(hashtagMapper.selectCount(queryWrapper));

        // 若匹配数量等于传入 ID 数量，则全部存在
        return count == idList.size();
    }


    @Override
    public HashTag getById(Integer id) {
        return hashtagMapper.selectById(id);
    }

    @Override
    public HashTag createHashtag(String name) {
        HashTag exist = hashtagMapper.selectByName(name);
        if (exist != null) {
            return exist;
        }
        HashTag hashtag = new HashTag();
        hashtag.setName(name);
        int rows = hashtagMapper.insertHashtag(hashtag);
        return hashtag;
    }

    @Override
    public boolean createHashtags(List<String> names) {
        for (String name : names) {
            HashTag exist = hashtagMapper.selectByName(name);
            if (exist != null) {
                continue;
            }
            HashTag hashtag = new HashTag();
            hashtag.setName(name);
            hashtagMapper.insertHashtag(hashtag);
        }
        return true;
    }

    @Override
    public boolean updateHashtag(HashTag hashtag) {
        int rows = hashtagMapper.updateHashtag(hashtag);
        return rows > 0;
    }

    @Override
    public List<HashTag> getHashtagsByName(String name) {
        return hashtagMapper.selectByNameLike(name);
    }
    @Override
    public List<HashTag> createByContent(String content1) {
        List<String> tags = tagExtractor.extractTagsFromContent(content1);
        createHashtags(tags);
        List<HashTag> result = new ArrayList<>();
        for(String tag:tags){
            HashTag hashtag = new HashTag();
            hashtag.setName(tag);
            HashTag hashtag1 =hashtagMapper.selectByName(tag);
            if(hashtag1==null){
                hashtagMapper.insertHashtag(hashtag);
            }else{
                hashtag.setHashtagId(hashtag1.getHashtagId());
            }
            result.add(hashtag);
        }
        return result;
    }
    @Override
    public Integer createHashtagAndReturnId(String name) {
        HashTag exist = hashtagMapper.selectByName(name);
        if (exist != null) {
            return exist.getHashtagId();
            // 也可以返回 null，视业务需求而定
        }

        HashTag hashtag = new HashTag();
        hashtag.setName(name);
        int rows = hashtagMapper.insertHashtag(hashtag);
        return rows > 0 ? hashtag.getHashtagId() : null;
    }


    @Override
    public List<HashTag> listAll() {
        return hashtagMapper.selectAll();
    }
    @Override
    public PageResult listHashtagsByPage(int page, int size) {
        int offset = (page - 1) * size;
        List<HashTag> records = hashtagMapper.selectPage(offset, size);
        long total = hashtagMapper.countTotal();
        return new PageResult(total, records);
    }


}
